<cfcomponent>

	<cffunction name="init" access="public" output="false" returntype="KalendarRequest" hint="I initialize this component">
		<cfargument name="AppAction" type="string" default="" />
		<cfargument name="CategoryID" type="string" default="" />
		<cfargument name="isAdminMode" type="string" default="false" />
		<cfargument name="LinkDate" type="string" default="" />
		<cfargument name="View" type="string" default="" />
		<cfset setAppAction( arguments.AppAction ) />
		<cfset setCategoryID( arguments.CategoryID ) />
		<cfset setIsAdminMode( arguments.isAdminMode ) />
		<cfset setLinkDate( arguments.LinkDate ) />
		<cfset setView( arguments.View ) />
		
		<cfreturn this />
		
	</cffunction>
	
	<cffunction name="getAppAction" access="public" output="false" returntype="string">
		<cfreturn variables.instance.AppAction />
	</cffunction>
	
	<cffunction name="setAppAction" access="public" output="false" returntype="void">
		<cfargument name="AppAction" type="string" required="true" />
		<cfset variables.instance.AppAction = arguments.AppAction />
	</cffunction>
	
	<cffunction name="getCategoriesRS" access="public" output="false" returntype="any">
		<cfreturn getCategoryDAO().getByAttributesQuery(orderBy="CategoryName") />
	</cffunction>
	
	<cffunction name="getCategoryID" access="public" output="false" returntype="string">
			<cfreturn val(variables.instance.CategoryID) />
	</cffunction>
	
	<cffunction name="setCategoryID" access="public" output="false" returntype="void">
		<cfargument name="CategoryID" type="string" required="true" />
		<cfset variables.instance.CategoryID = arguments.CategoryID />
	</cffunction>

	<cffunction name="getIsAdminMode" access="public" output="false" returntype="string">
		<cfreturn variables.instance.IsAdminMode />
	</cffunction>
	
	<cffunction name="setIsAdminMode" access="public" output="false" returntype="void">
		<cfargument name="IsAdminMode" type="string" required="true" />
		<cfset variables.instance.IsAdminMode = arguments.IsAdminMode />
	</cffunction>

	<cffunction name="getLinkDate" access="public" output="false" returntype="string">
		<cfset var rtnDate = now() />
		<cfif isDate( variables.instance.LinkDate ) >
			<cfset rtnDate = variables.instance.LinkDate />
		</cfif>
		<cfreturn rtnDate />
	</cffunction>
	
	<cffunction name="setLinkDate" access="public" output="false" returntype="void">
		<cfargument name="LinkDate" type="string" required="true" />
		<cfset variables.instance.LinkDate = arguments.LinkDate />
	</cffunction>

	<cffunction name="getDBFormatLinkDate" access="public" output="false" returntype="string">
		<cfset var rtnVal = "" />
		<cfif isDate( getLinkDate() ) >
			<cfset rtnVal = CreateODBCDate( getLinkDate() ) />	
		</cfif>
		
		<cfreturn rtnVal />
		
	</cffunction>

	<cffunction name="getLongLinkDate" access="public" output="false" returntype="string">
		<cfset var rtnVal = "" />
		<cfif isDate( getLinkDate() ) >
			<cfset rtnVal = dateformat( getLinkDate(), getKalendarConfig().getLongDateMask() ) />	
		</cfif>
		
		<cfreturn rtnVal />
		
	</cffunction>

	<cffunction name="getShortLinkDate" access="public" output="false" returntype="string">
		<cfset var rtnVal = "" />
		<cfif isDate( getLinkDate() ) >
			<cfset rtnVal = dateformat( getLinkDate(), getKalendarConfig().getShortDateMask() ) />	
		</cfif>
		
		<cfreturn rtnVal />
		
	</cffunction>

	<cffunction name="getFirstDisplayDateOfMonth" access="public" output="false" returntype="string">
		<cfreturn CreateODBCDate( DateAdd( 'd', -DayOfWeek( getFirstDateOfMonth() ) + 1, getFirstDateOfMonth() ) ) />
	</cffunction>
	
	<cffunction name="getFirstDateOfMonth" access="public" output="false" returntype="string">
		<cfreturn CreateODBCDate( Month( getLinkDate() ) & "/1/" &  Year( getLinkDate() ) ) />
	</cffunction>
	
	<cffunction name="getLastDateOfMonth" access="public" output="false" returntype="string">
		<cfreturn  CreateODBCDate( Month( getLinkDate() ) & "/" & DaysInMonth( getFirstDateOfMonth() ) & "/" &  Year( getLinkDate() )) />
	</cffunction>
	
	<cffunction name="getFirstDateOfWeek" access="public" output="false" returntype="string">
		<cfset var date = getLinkDate()/>
		<cfset date = CreateDateTime(Year(date), Month(date), Day(date), 0, 0, 0)/>
		<cfreturn DateAdd( 'd', 1 - DayOfWeek(date), date)/>
	</cffunction>
	
	<cffunction name="getLastDateOfWeek" access="public" output="false" returntype="string">
		<cfreturn DateAdd('d', 7, getFirstDateOfWeek()) />
	</cffunction>
	
	<cffunction name="getFirstWeekOfMonth" access="public" output="false" returntype="string">
		<cfreturn  Week( getFirstDateOfMonth() )/>
	</cffunction>
	
	<cffunction name="getLastWeekOfMonth" access="public" output="false" returntype="string">
		<cfreturn  Week( getLastDateOfMonth() )/>
	</cffunction>
	
	<cffunction name="getStartPeriod" access="public" output="false" returntype="string">
		<cfset var mode = getInternalView() />
		<cfset var date = getFirstDateOfMonth() />
		<cfswitch expression="#mode#">
			<cfcase value="day">
				<cfreturn CreateODBCDateTime(DateFormat( getLinkDate(), "m/d/yyyy") & " 12:00:00 AM") />	
			</cfcase>
			<cfcase value="week">
				<cfreturn CreateODBCDateTime(DateFormat( getFirstDateOfWeek(), "m/d/yyyy") & " 12:00:00 AM") >
			</cfcase>
		</cfswitch>
		<cfreturn date />
	</cffunction>
	
	<cffunction name="getEndPeriod" access="public" output="false" returntype="string">
		<cfset var mode = getInternalView() />
		<cfset var date = getLastDateOfMonth() />
		<cfswitch expression="#mode#">
		<cfcase value="day">
				<cfreturn CreateODBCDateTime(DateFormat( getLinkDate(), "m/d/yyyy") & " 11:59:59 PM") />	
			</cfcase>
			<cfcase value="week">
				<cfreturn CreateODBCDateTime(DateFormat( getLastDateOfWeek(), "m/d/yyyy") & " 11:59:59 PM") >
			</cfcase>
		</cfswitch>
		<cfreturn date />
	</cffunction>
	
	
	<cffunction name="getReturnedEvents" access="public" output="true" returntype="array">
		<cfargument name="User" type="User" required="true"/>
		<cfargument name="timezones" type="Timezones" required="true"/>
		<cfreturn getKalendarDAO().getCalendar( view=getInternalView(), startPeriod=arguments.timezones.castToUTC(getStartPeriod(),arguments.user.getTimezone()), endPeriod=arguments.timezones.castToUTC(getEndPeriod(),arguments.user.getTimezone()), User=arguments.user, categoryID=getCategoryID(), timezones=arguments.timezones) />
	
	</cffunction>

	<cffunction name="getReturnedEventsXML" access="public" output="false" returntype="string">
		<cfreturn getKalendarDAO().loadKalendarXML( view=getInternalView(), linkDate=getLinkDate(), startPeriod=getStartPeriod(), endPeriod=getEndPeriod(), categoryID=getCategoryID() ) />
	
	</cffunction>


	<cffunction name="getView" access="public" output="false" returntype="string">
		<cfif listFindNoCase( getKalendarConfig().getValidViewList(), variables.instance.View ) >
			<cfreturn variables.instance.View /> 
		</cfif>

		<cfreturn getKalendarConfig().getDefaultView() />
			
	</cffunction>
	
	<cffunction name="getInternalView" access="public" output="false" returntype="string">
		<cfset var rtnVal = getKalendarConfig().getDefaultView() />

		<cfif listFindNoCase( getKalendarConfig().getValidViewList(), variables.instance.View ) >
			<cfset rtnVal = variables.instance.View />
			<cfif variables.instance.View IS "list" >
				<cfset rtnVal = "month" />
			</cfif>
			<cfif variables.instance.View IS "weeklist">
				<cfset rtnVal = "week" />
			</cfif>
		</cfif>
			
		<cfreturn rtnVal />
			
	</cffunction>
	
	<cffunction name="setView" access="public" output="false" returntype="void">
		<cfargument name="View" type="string" required="true" />
		<cfset variables.instance.View = arguments.View />
	</cffunction>
	
	<cffunction name="getKalendarConfig" access="public" output="false" returntype="any">
		<cfreturn variables.instance.KalendarConfig />
	</cffunction>
	
	<cffunction name="setKalendarConfig" access="public" output="false" returntype="void">
		<cfargument name="KalendarConfig" type="any" required="true" />
		<cfset variables.instance.KalendarConfig = arguments.KalendarConfig />
	</cffunction>

	<cffunction name="getKalendarDAO" access="public" output="false" returntype="any">
		<cfreturn variables.instance.KalendarDAO />
	</cffunction>
	
	<cffunction name="setKalendarDAO" access="public" output="false" returntype="void">
		<cfargument name="KalendarDAO" type="any" required="true" />
		<cfset variables.instance.KalendarDAO = arguments.KalendarDAO />
	</cffunction>
	
	<cffunction name="getCategoryDAO" access="public" output="false" returntype="any">
		<cfreturn variables.instance.CategoryDAO />
	</cffunction>
	
	<cffunction name="setCategoryDAO" access="public" output="false" returntype="void">
		<cfargument name="CategoryDAO" type="any" required="true" />
		<cfset variables.instance.CategoryDAO = arguments.CategoryDAO />
	</cffunction>
	
	<cffunction name="setTimezones" access="public" output="false" returntype="void">
		<cfargument name="Timezone" type="Timezones" required="true"/>
		<cfset variables.instance.timezones = arguments.Timezone/>
	</cffunction>
	
	<cffunction name="getTimezones" access="public" output="false" returntype="Timezones">
		<cfreturn variables.instance.timezones />
	</cffunction>
	
</cfcomponent>